/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package mxi.its.dds2.Bean;

import java.util.ArrayList;
import java.util.List;
import javax.ejb.EJB;
import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import javax.faces.context.FacesContext;
import mx.its.dsw2.biblioteca.Autor;
import mx.its.dsw2.biblioteca.bl.AutorBlLocal;
import org.primefaces.context.RequestContext;
import org.primefaces.event.SelectEvent;
import org.primefaces.event.ToggleEvent;
import org.primefaces.event.UnselectEvent;

/**
 *
 * @author Carlos Asis
 */
@ManagedBean
@SessionScoped
public class AuthorBean {
    @EJB
    private AutorBlLocal authorBl;
    
    private Autor autor;
    private List<Autor> list;
    private List<Autor> filteredAuthor;

    public Autor getAutor() {
        return autor;
    }

    public void setAutor(Autor autor) {
        this.autor = autor;
    }
    
    
    public List<Autor> getList() {
        list = authorBl.getListAll();
        return list;
    }
    
    public void setList(List<Autor> list) {
        this.list = list;
    }
    
    public List<Autor> getFilteredUser() {
        return filteredAuthor;
    }
    
    public void setFilteredUser(List<Autor> filteredAuthor) {
        this.filteredAuthor = filteredAuthor;
    }
    
    public String prepararAutor() {
        autor = new Autor();
        return "AutorCrearEditar";
    }

    /////////////////////Para el registro author saber cuando se abre//////////////
    public String crearAutor() {
        
        authorBl.register(autor);
        return "AuthorFacelets";
    }

    public void handleToggle(ToggleEvent event) {
        
        if (event.getVisibility().name().equals("VISIBLE")) {
            FacesMessage message = new FacesMessage(FacesMessage.SEVERITY_INFO, event.getComponent().getId() + " toggled", "Status:" + event.getVisibility().name());
            addMessage(message);
            // this.reset();
            autor = new Autor();
        } else if (event.getVisibility().name().equals("HIDDEN")) {
            this.reset();
            autor = new Autor();
        } else {
            
        }
    }
    
    private void addMessage(FacesMessage message) {
        FacesContext.getCurrentInstance().addMessage(null, message);
        
        
    }
    
    public void reset() {
        RequestContext.getCurrentInstance().reset("formm:pnll");
        
    }

    /////////////termina 
    //////////////Editar datos de la tabla///////////////
    public void onEdit(SelectEvent event) {
        FacesMessage msg = new FacesMessage("Car Selected", autor.getNombre());
        
        FacesContext.getCurrentInstance().addMessage(null, msg);
    }
    
    public void onRowUnselect(UnselectEvent event) {
        FacesMessage msg = new FacesMessage("Car Unselected", ((Autor) event.getObject()).getNombre());
        
        FacesContext.getCurrentInstance().addMessage(null, msg);
    }
    
    public String editUser() {
        authorBl.modify(autor);
        return "AutorFacelets";
    }

    ////////////////////////terminba edicion
    /**
     * Creates a new instance of AutorBean
     */
    public AuthorBean(){
        list = new ArrayList<Autor>();
        autor=new Autor();
                
        //list=authorBl.getListAll();
    }
}
